<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>作用域链 | JavaScript 高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.2.0">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
    

        
    
    
    <link rel="next" href="../e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html" />
    
    
    <link rel="prev" href="../e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="1.6" data-basepath=".." data-revision="Tue Mar 01 2016 06:46:18 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        JavaScript 高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="e9c9c586570ff813bf127694b025d155/index.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        面向对象基础复习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        原型的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        继承的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        Object 对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        Function 对象
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.6" data-path="e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        作用域链
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        闭包的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        getter 与 setter
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="e2bb52a93f2a04d3848c6bc059d4442d/index.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        面向对象高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        原型链
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        函数的四中调用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        对象的创建模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        继承的方式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        闭包案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        正则表达式对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        XMLHttpRequest 对象
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >JavaScript 高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="%E4%BD%9C%E7%94%A8%E5%9F%9F%E9%93%BE">&#x4F5C;&#x7528;&#x57DF;&#x94FE;</h1>
<p>&#x4E3B;&#x8981;&#x5185;&#x5BB9;:</p>
<ul>
<li>&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;&#x4E0E;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;</li>
<li>&#x51FD;&#x6570;&#x9650;&#x5B9A;&#x4F5C;&#x7528;&#x57DF;</li>
<li>&#x4F5C;&#x7528;&#x57DF;&#x94FE;</li>
<li>&#x53D8;&#x91CF;&#x540D;&#x63D0;&#x5347;&#x4E0E;&#x51FD;&#x6570;&#x540D;&#x63D0;&#x5347;</li>
</ul>
<p>JavaScript &#x4F5C;&#x7528;&#x57DF;&#x7684;&#x95EE;&#x9898;&#x662F;&#x9762;&#x8BD5;&#x9898;&#x4E2D;&#x5E38;&#x8003;&#x7684;&#x4E00;&#x4E2A;&#x6280;&#x5DE7;, &#x4E5F;&#x662F;&#x5F00;&#x53D1;&#x4E2D;&#x5E38;&#x5E38;&#x51FA;&#x73B0;&#x95EE;&#x9898;&#x7684;&#x5730;&#x65B9;. &#x672C;&#x8282;&#x9010;&#x6B65;&#x5206;&#x6790;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x95EE;&#x9898;, &#x540C;&#x65F6;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A;&#x5224;&#x65AD;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x901A;&#x7528;&#x65B9;&#x6CD5;.</p>
<h2 id="%E5%9D%97%E7%BA%A7%E4%BD%9C%E7%94%A8%E5%9F%9F%E7%9A%84%E6%A6%82%E5%BF%B5">&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x6982;&#x5FF5;</h2>
<p>&#x5728; C &#x7CFB;&#x7684;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E2D;, &#x57FA;&#x672C;&#x4E0A;&#x90FD;&#x6709;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;. &#x6240;&#x8C13;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;, &#x5C31;&#x662F;&#x5229;&#x7528;&#x4EE3;&#x7801;&#x5757;&#x6765;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x7684;&#x8303;&#x56F4;. &#x5728;&#x8BE5;&#x8303;&#x56F4;&#x5185;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;, &#x53EA;&#x5141;&#x8BB8;&#x5728;&#x8BE5;&#x8303;&#x56F4;&#x5185;&#x4F7F;&#x7528;, &#x8BE5;&#x8303;&#x56F4;&#x5916;&#x5C31;&#x627E;&#x4E0D;&#x5230;&#x53D8;&#x91CF;. &#x800C;&#x8FD9;&#x4E2A;&#x8303;&#x56F4;&#x4F7F;&#x7528;&#x82B1;&#x62EC;&#x53F7;&#x6765;&#x9650;&#x5B9A;. &#x4F8B;&#x5982;:</p>
<pre><code class="lang-c">{
    <span class="hljs-keyword">int</span> num = <span class="hljs-number">123</span>;
}
<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;num = %d\n&quot;</span>, num);
</code></pre>
<p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x5C31;&#x4F1A;&#x62A5;&#x9519;. &#x56E0;&#x4E3A; <code>num</code> &#x5B9A;&#x4E49;&#x5728;&#x82B1;&#x62EC;&#x53F7;&#x4E2D;, &#x53EA;&#x5141;&#x8BB8;&#x5728;&#x8BE5;&#x82B1;&#x62EC;&#x53F7;&#x4E2D;&#x88AB;&#x8BBF;&#x95EE;. &#x4F46;&#x662F;&#x5728; JavaScript &#x4E2D;&#x5C31;&#x4E0D;&#x662F;&#x8FD9;&#x6837;.</p>
<h2 id="javascript-%E6%B2%A1%E6%9C%89%E5%9D%97%E7%BA%A7%E4%BD%9C%E7%94%A8%E5%9F%9F">JavaScript &#x6CA1;&#x6709;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;</h2>
<p>&#x5728; JavaScript &#x4E2D;, &#x4EE3;&#x7801;</p>
<pre><code class="lang-javascript">{
    int num = <span class="hljs-number">123</span>;
}
<span class="hljs-built_in">console</span>.log(num);
</code></pre>
<p>&#x53EF;&#x4EE5;&#x6B63;&#x5E38;&#x8FD0;&#x884C;. &#x90A3;&#x4E48;&#x5728; JavaScript &#x4E2D;&#x4F5C;&#x7528;&#x57DF;&#x662F;&#x5982;&#x4F55;&#x9650;&#x5B9A;&#x7684;&#x5462;?</p>
<h3 id="%E5%87%BD%E6%95%B0%E9%99%90%E5%AE%9A%E5%8F%98%E9%87%8F%E4%BD%9C%E7%94%A8%E5%9F%9F">&#x51FD;&#x6570;&#x9650;&#x5B9A;&#x53D8;&#x91CF;&#x4F5C;&#x7528;&#x57DF;</h3>
<p>&#x5728; JavaScript &#x4E2D;, &#x53EA;&#x6709;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x9650;&#x5B9A;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x4F5C;&#x7528;&#x8303;&#x56F4;. &#x4EC0;&#x4E48;&#x610F;&#x601D;&#x5462;? &#x5C31;&#x662F;&#x8BF4;, &#x5728; JavaScript &#x4E2D;, &#x5728;&#x51FD;&#x6570;&#x91CC;&#x9762;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;, &#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x91CC;&#x9762;&#x88AB;&#x8BBF;&#x95EE;, &#x4F46;&#x662F;&#x5728;&#x51FD;&#x6570;&#x5916;&#x65E0;&#x6CD5;&#x8BBF;&#x95EE;. &#x770B;&#x5982;&#x4E0B;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
}

<span class="hljs-keyword">try</span> {
    <span class="hljs-built_in">console</span>.log(num);
} <span class="hljs-keyword">catch</span>(e) {
    <span class="hljs-built_in">console</span>.log(e);
}
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;:</p>
<p><img src="../images/Snip20160224_31.png" alt=""></p>
<p>&#x53EF;&#x89C1;&#x4E0E;&#x4E4B;&#x524D;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x6548;&#x679C;&#x4E00;&#x6837;&#x62A5;&#x9519;. &#x518D;&#x6765;&#x770B;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(num);
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
    <span class="hljs-built_in">console</span>.log(num);
}

<span class="hljs-keyword">try</span> {
    func();
} <span class="hljs-keyword">catch</span>(e) {
    <span class="hljs-built_in">console</span>.log(e);
}
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;:</p>
<p><img src="../images/Snip20160224_32.png" alt=""></p>
<p>&#x56E0;&#x6B64;, &#x5B9A;&#x4E49;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x53D8;&#x91CF;, &#x53EA;&#x5141;&#x8BB8;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x88AB;&#x8BBF;&#x95EE;.</p>
<p>&#x95EE;&#x9898;: &#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x4F1A;&#x62A5;&#x9519;&#x5417;? &#x5982;&#x679C;&#x4E0D;&#x4F1A;&#x5E94;&#x8BE5;&#x6709;&#x4EC0;&#x4E48;&#x7ED3;&#x679C;? &#x4E3A;&#x4EC0;&#x4E48;?</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">func</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(num);
    }
    func();
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
}
foo();
</code></pre>
<p>&#x6709;&#x4E86;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x5B58;&#x5728;, &#x53EF;&#x4EE5;&#x5229;&#x7528;&#x81EA;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x6765;&#x6A21;&#x62DF;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;.</p>
<h3 id="%E5%AD%90%E5%9F%9F%E8%AE%BF%E9%97%AE%E7%88%B6%E5%9F%9F">&#x5B50;&#x57DF;&#x8BBF;&#x95EE;&#x7236;&#x57DF;</h3>
<p>&#x524D;&#x9762;&#x4ECB;&#x7ECD;&#x4E86;, &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x9650;&#x5B9A;&#x53D8;&#x91CF;&#x7684;&#x4F5C;&#x7528;&#x57DF;, &#x90A3;&#x4E48;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x518D;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x51FD;&#x6570;, &#x5C31;&#x662F;&#x5728;&#x4E00;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x53C8;&#x6709;&#x4E00;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;, &#x79F0;&#x4E3A;&#x8BE5;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x5B50;&#x57DF;.</p>
<p>&#x6709;&#x4E00;&#x4E2A;&#x7ED3;&#x8BBA;: &#x5B50;&#x57DF;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;&#x7236;&#x57DF;&#x4E2D;&#x7684;&#x53D8;&#x91CF;.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">10</span>;
    <span class="hljs-keyword">var</span> sub_func = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(num);
    };
    sub_func();
};
func();
</code></pre>
<p>&#x770B;&#x6267;&#x884C;&#x7ED3;&#x679C;:</p>
<p><img src="../images/Snip20160224_33.png" alt=""></p>
<p>&#x53EF;&#x89C1; <code>sub_func</code> &#x4E2D;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE; <code>func</code> &#x4E2D;&#x7684;&#x53D8;&#x91CF;. &#x89C4;&#x5F8B;&#x548C;&#x660E;&#x663E;, &#x51FD;&#x6570;&#x9650;&#x5B9A;&#x4F5C;&#x7528;&#x57DF;, &#x53EA;&#x8981;&#x662F;&#x5728;&#x8BE5;&#x51FD;&#x6570;&#x4E2D;&#x5C31;&#x53EF;&#x4EE5;&#x88AB;&#x8BBF;&#x95EE;. &#x4F46;&#x662F;&#x5B50;&#x57DF;&#x8BBF;&#x95EE;&#x4E5F;&#x662F;&#x6709;&#x4E00;&#x5B9A;&#x6761;&#x4EF6;&#x7684;, &#x770B;&#x4E0B;&#x9762;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">10</span>;
    <span class="hljs-keyword">var</span> sub_func = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">var</span> num = <span class="hljs-number">20</span>;
        <span class="hljs-built_in">console</span>.log(num);
    };
    sub_func();
};
func();
</code></pre>
<p>&#x8FD0;&#x884C;&#x7684;&#x7ED3;&#x679C;&#x4E3A; 20. &#x56E0;&#x4E3A;&#x5728; <code>sub_func</code> &#x7684;&#x4EE3;&#x7801;&#x4E2D;&#x6709; <code>var</code>. &#x8FD9;&#x4E2A;&#x662F;&#x58F0;&#x660E;&#x53D8;&#x91CF;, &#x56E0;&#x6B64;&#x5728;&#x5B50;&#x57DF;&#x4E2D;&#x58F0;&#x660E;&#x4E86;&#x4E00;&#x4E2A;&#x53D8;&#x91CF; <code>num</code>, &#x90A3;&#x4E48;&#x7236;&#x57DF;&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#x5C31;&#x4F1A;&#x88AB;&#x8986;&#x76D6;&#x6389;. &#x56E0;&#x6B64;&#x6253;&#x5370;&#x7684;&#x7ED3;&#x679C;&#x662F; 20.</p>
<p>&#x7531;&#x6B64;&#x53EF;&#x89C1;&#x8BBF;&#x95EE;&#x6709;&#x4E00;&#x5B9A;&#x89C4;&#x5219;&#x53EF;&#x8A00;. &#x5728; JavaScript &#x4E2D;&#x4F7F;&#x7528;&#x53D8;&#x91CF;, JavaScript &#x89E3;&#x91CA;&#x5668;&#x9996;&#x5148;&#x5728;&#x5F53;&#x524D;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x641C;&#x7D22;&#x662F;&#x5426;&#x6709;&#x8BE5;&#x53D8;&#x91CF;&#x7684;&#x5B9A;&#x4E49;. &#x5982;&#x679C;&#x6709;, &#x5C31;&#x662F;&#x7528;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#xFF1B;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5C31;&#x5230;&#x7236;&#x57DF;&#x4E2D;&#x5BFB;&#x627E;&#x8BE5;&#x53D8;&#x91CF;. &#x4EE5;&#x6B64;&#x7C7B;&#x63A8;, &#x76F4;&#x5230;&#x6700;&#x9876;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;, &#x4ECD;&#x7136;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x5C31;&#x629B;&#x51FA;&#x5F02;&#x5E38;&quot;&#x53D8;&#x91CF;&#x672A;&#x5B9A;&#x4E49;&quot;. &#x770B;&#x4E0B;&#x9762;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript">(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;
    (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">var</span> num = <span class="hljs-number">456</span>;
        (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-built_in">console</span>.log(num);
        })();
    })();
})();
</code></pre>
<p>&#x8FD9;&#x91CC;&#x6267;&#x884C;, &#x5728;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x4E2D;&#x6CA1;&#x6709;&#x627E;&#x5230; <code>num</code>, &#x56E0;&#x6B64;&#x5411;&#x4E0A;&#x67E5;&#x627E;, &#x6253;&#x5370;&#x7684;&#x662F; 456, &#x5982;&#x679C;&#x5C06; <code>var num = 456;</code> &#x6CE8;&#x91CA;&#x6389;, &#x5219;&#x7EE7;&#x7EED;&#x5411;&#x4E0A;&#x627E;, &#x6253;&#x5370; 123, &#x5982;&#x679C;&#x5C06; <code>var num = 123;</code> &#x6CE8;&#x91CA;&#x6389;, &#x90A3;&#x4E48;&#x8FD8;&#x662F;&#x5411;&#x4E0A;&#x627E;, &#x627E;&#x5230;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;, &#x5982;&#x679C;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x672A;&#x5B9A;&#x4E49;&#x53D8;&#x91CF; <code>num</code> &#x90A3;&#x4E48;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;.</p>
<!--
    词法作用域

foo();
var a = true; if(a){
function foo() { console.log("a"); } }
else {
function foo() { console.log("b"); }
}




function foo(count) {
    var arr = [];
    var i = 0;

    for (; i < count; i++) {
        arr[i] = function() {
            return i;
        }
    }
    return arr;
}

-->
<h2 id="%E4%BD%9C%E7%94%A8%E5%9F%9F%E9%93%BE">&#x4F5C;&#x7528;&#x57DF;&#x94FE;</h2>
<p>&#x6709;&#x4E86; JavaScript &#x7684;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x5212;&#x5206;, &#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x5C06; JavaScript &#x7684;&#x8BBF;&#x95EE;&#x4F5C;&#x7528;&#x57DF;&#x8FDE;&#x6210;&#x4E00;&#x4E2A;&#x94FE;&#x5F0F;&#x6811;&#x72B6;&#x7ED3;&#x6784;. JavaScript &#x7684;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x4E00;&#x65E6;&#x80FD;&#x6E05;&#x6670;&#x7684;&#x4E86;&#x89E3;, &#x90A3;&#x4E48;&#x5BF9;&#x4E8E; JavaScript &#x7684;&#x53D8;&#x91CF;&#x4E0E;&#x95ED;&#x5305;&#x5C31;&#x662F;&#x975E;&#x5E38;&#x6E05;&#x6670;&#x7684;&#x4E86;.</p>
<p>&#x4E0B;&#x9762;&#x91C7;&#x7528;&#x7ED8;&#x56FE;&#x7684;&#x65B9;&#x6CD5;, &#x7ED8;&#x5236;&#x51FA;&#x4F5C;&#x7528;&#x57DF;&#x94FE;.</p>
<p><strong>&#x7ED8;&#x5236;&#x89C4;&#x5219;</strong></p>
<ol>
<li>&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x5C31;&#x662F;&#x5BF9;&#x8C61; ( &#x53D8;&#x91CF; ) &#x7684;&#x6570;&#x7EC4;</li>
<li>&#x5168;&#x90E8;&#x7684;&#x6574;&#x4E2A; script &#x662F; 0 &#x7EA7;&#x94FE;, &#x6BCF;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x5360;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;</li>
<li>&#x51E1;&#x662F;&#x770B;&#x5230;&#x4E00;&#x4E2A;&#x51FD;&#x6570;, &#x5C31;&#x5EF6;&#x5C55;&#x4E00;&#x4E2A;&#x94FE;, &#x4E00;&#x7EA7;&#x7EA7;&#x5C55;&#x5F00;</li>
<li>&#x8BBF;&#x95EE;&#x9996;&#x5148;&#x770B;&#x5F53;&#x524D;&#x51FD;&#x6570;, &#x5982;&#x679C;&#x6CA1;&#x6709;&#x5B9A;&#x4E49;&#x5F80;&#x4E0A;&#x4E00;&#x7EA7;&#x94FE;&#x770B;</li>
<li>&#x5982;&#x6B64;&#x5F80;&#x590D;, &#x76F4;&#x5230; 0 &#x7EA7;&#x94FE;</li>
</ol>
<p><strong>&#x4E3E;&#x4F8B;</strong></p>
<p>&#x770B;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> num = <span class="hljs-number">10</span>;
<span class="hljs-keyword">var</span> func1 = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">20</span>;
    <span class="hljs-keyword">var</span> func2 = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">var</span> num = <span class="hljs-number">30</span>;
        <span class="hljs-built_in">console</span>.log(num);
    };
    func2();
};
<span class="hljs-keyword">var</span> func2 = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> num = <span class="hljs-number">20</span>;
    <span class="hljs-keyword">var</span> func3 = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(num);
    };
    func3();
};
func1();
func2();
</code></pre>
<p>&#x5206;&#x6790;&#x4E00;&#x4E0B;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;:</p>
<ul>
<li>&#x9996;&#x5148;&#x6574;&#x6BB5;&#x4EE3;&#x7801;&#x662F;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;, &#x53EF;&#x4EE5;&#x6807;&#x8BB0;&#x4E3A; 0 &#x7EA7;&#x522B;&#x94FE;, &#x90A3;&#x4E48;&#x5C31;&#x6709;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;:</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> link_0 = [num, func1, func2];
</code></pre>
<ul>
<li>&#x8FD9;&#x91CC; <code>func1</code> &#x548C; <code>func2</code> &#x662F;&#x4E24;&#x4E2A;&#x51FD;&#x6570;, &#x56E0;&#x6B64;&#x5F15;&#x51FA;&#x4E24;&#x6761; 1 &#x7EA7;&#x94FE;, &#x5206;&#x522B;&#x4E3A;:</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> link_1 = { func1: [num, func2] };
<span class="hljs-keyword">var</span> link_1 = { func2: [num, func3] };
</code></pre>
<ul>
<li>&#x7B2C;&#x4E00;&#x6761; 1 &#x7EA7;&#x94FE;, &#x5F15;&#x51FA; 2 &#x7EA7;&#x94FE;:</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> link_2 = { func2: [num] };
</code></pre>
<ul>
<li>&#x7B2C;&#x4E8C;&#x6761; 1 &#x7EA7;&#x94FE;, &#x4E5F;&#x5F15;&#x51FA;&#x4E00;&#x6761; 2 &#x7EA7;&#x94FE;, &#x8BE5;&#x51FD;&#x6570;&#x4E2D;&#x672A;&#x5B9A;&#x4E49;&#x53D8;&#x91CF;, &#x56E0;&#x6B64;&#x4E3A;&#x7A7A;&#x6570;&#x7EC4;:</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> link_2 = { func3: [] };
</code></pre>
<ul>
<li>&#x5C06;&#x4F2A;&#x4EE3;&#x7801;&#x6574;&#x5408;&#x4E00;&#x4E0B;:</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> link = [    <span class="hljs-comment">// 0 &#x7EA7;&#x94FE;</span>
    num,
    { func1: [  <span class="hljs-comment">// 1 &#x7EA7;&#x94FE;</span>
        num,
        { func2: [num] }    <span class="hljs-comment">// 2 &#x7EA7;&#x94FE;</span>
    ] },
    { func2: [  <span class="hljs-comment">// 1 &#x7EA7;&#x94FE;</span>
        num,
        { func3: [] }       <span class="hljs-comment">// 2&#x7EA7;&#x94FE;</span>
    ] }
];
</code></pre>
<p>&#x7ED8;&#x6210;&#x56FE;&#x4E3A;:</p>
<p><img src="../images/Snip20160225_34.png" alt=""></p>
<p>&#x6709;&#x4E86;&#x8FD9;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x7684;&#x56FE;, &#x90A3;&#x4E48;&#x5C31;&#x53EF;&#x4EE5;&#x975E;&#x5E38;&#x6E05;&#x6670;&#x7684;&#x4E86;&#x89E3;&#x8BBF;&#x95EE;&#x53D8;&#x91CF;&#x662F;&#x5982;&#x4F55;&#x8FDB;&#x884C;&#x7684;:</p>
<p>&#x5728;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x53D8;&#x91CF;&#x65F6;, &#x9996;&#x5148;&#x5728;&#x5F53;&#x524D;&#x7684;&#x94FE;&#x4E0A;&#x5BFB;&#x627E;&#x53D8;&#x91CF;, &#x5982;&#x679C;&#x627E;&#x5230;&#x5C31;&#x76F4;&#x63A5;&#x4F7F;&#x7528;, &#x4E0D;&#x4F1A;&#x5411;&#x4E0A;&#x518D;&#x627E;; &#x5982;&#x679C;&#x6CA1;&#x6709;&#x627E;&#x5230;, &#x90A3;&#x4E48;&#x5C31;&#x5411;&#x4E0A;&#x4E00;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x5BFB;&#x627E;, &#x76F4;&#x5230;0&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x94FE;.</p>
<p>&#x5982;&#x679C;&#x80FD;&#x975E;&#x5E38;&#x6E05;&#x6670;&#x7684;&#x786E;&#x5B9A;&#x53D8;&#x91CF;&#x6240;&#x5C5E;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x7684;&#x7EA7;&#x522B;, &#x90A3;&#x4E48;&#x5728;&#x5206;&#x6790; JavaScript &#x4EE3;&#x7801;&#x4E0E;&#x4F7F;&#x7528;&#x95ED;&#x5305;&#x7B49;&#x9AD8;&#x7EA7; JavaScript &#x7279;&#x6027;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x4F1A;&#x975E;&#x5E38;&#x5BB9;&#x6613;.</p>
<p>&#x601D;&#x8003;: &#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x662F;&#x4EC0;&#x4E48;?</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span> (! <span class="hljs-string">&quot;a&quot;</span> <span class="hljs-keyword">in</span> <span class="hljs-built_in">window</span>) {
    <span class="hljs-keyword">var</span> a = <span class="hljs-string">&quot;&#x5B9A;&#x4E49;&#x53D8;&#x91CF;&quot;</span>;
}
<span class="hljs-built_in">console</span>.log(a);
</code></pre>
<h2 id="%E5%B0%8F%E7%BB%93">&#x5C0F;&#x7ED3;</h2>
<ul>
<li>&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;</li>
<li>&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;</li>
<li>&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x4F5C;&#x7528;&#x57DF;</li>
<li>&#x5982;&#x4F55;&#x7ED8;&#x5236;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x5206;&#x6790;&#x4EE3;&#x7801;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html" class="navigation navigation-prev " aria-label="Previous page: Function 对象"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html" class="navigation navigation-next " aria-label="Next page: 闭包的基本概念"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
